<?php
include("../inc/Function_user.php");
upower();
$info = userinfo();
if($info['level']<1){ echo "<script>alert('权限不足');window.location.href='/user/';</script>";die;}

// 获取团队成员数量 - 参考 team.php
$team_count = isset($info['teamnum']) ? $info['teamnum'] : 0;

// 获取余额
$balance = isset($info['cash']) ? $info['cash'] : 0;
$balance_display = $balance;
if (is_numeric($balance)) {
    $balance_display = number_format(floatval($balance), 0, '.', '');
}

// 获取累计收益 - 可以使用团队业绩或其他收益字段
$total_earnings = isset($info['teamorder']) ? $info['teamorder'] : 0;

// 获取收益明细与提现记录
$psize = 20;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$earning_list = cashlist($page, $psize);
$withdraw_list = txlist($page, $psize);

$transactions = array();

if($earning_list){
    foreach($earning_list as $item){
        $label = '';
        if(!empty($item['content'])){
            $label = $item['content'];
        }elseif(isset($jjarr) && isset($item['type']) && isset($jjarr[$item['type']])){
            $label = $jjarr[$item['type']];
        }else{
            $label = '收益变动';
        }

        $isIncome = isset($item['adda']) && intval($item['adda']) == 1;
        $sign = $isIncome ? '+' : '-';
        $amountClass = $isIncome ? 'positive' : 'negative';

        $transactions[] = array(
            'amount' => number_format(abs($item['cash']), 2, '.', ''),
            'sign' => $sign,
            'amount_class' => $amountClass,
            'time' => $item['addtime'],
            'label' => $label,
            'detail' => ''
        );
    }
}

if($withdraw_list){
    foreach($withdraw_list as $item){
        $statusText = '';
        if(isset($txarr) && isset($item['state']) && isset($txarr[$item['state']])){
            $statusText = $txarr[$item['state']];
        }
        $displayAmount = isset($item['num']) ? $item['num'] : (isset($item['cash']) ? $item['cash'] : 0);
        $detail = '';
        if(isset($item['cash']) && isset($item['num']) && $item['cash'] != $item['num']){
            $detail = '到账¥'.number_format($item['cash'], 2, '.', '');
        }

        $transactions[] = array(
            'amount' => number_format(abs($displayAmount), 2, '.', ''),
            'sign' => '-',
            'amount_class' => 'negative',
            'time' => $item['addtime'],
            'label' => '收益提现'.($statusText ? ' '.$statusText : ''),
            'detail' => $detail
        );
    }
}

if($transactions){
    usort($transactions, function($a, $b){
        $ta = isset($a['time']) ? strtotime($a['time']) : 0;
        $tb = isset($b['time']) ? strtotime($b['time']) : 0;
        if($ta == $tb){
            return 0;
        }
        return $ta > $tb ? -1 : 1;
    });
    if(count($transactions) > $psize){
        $transactions = array_slice($transactions, 0, $psize);
    }
}

// 获取邀请记录 - 参考 team.php
$invitation_page = isset($_GET['ipage']) ? intval($_GET['ipage']) : 1;
$invitation_list = userlist($invitation_page, $psize, $info['Id']);
if(!$invitation_list){
    $invitation_list = array();
}

// 分配变量到模板
$tpl->assign("info", $info);
$tpl->assign("team_count", $team_count);
$tpl->assign("balance", $balance);
$tpl->assign("balance_display", $balance_display);
$tpl->assign("total_earnings", $total_earnings);
$tpl->assign("transaction_list", $transactions);
$tpl->assign("invitation_list", $invitation_list);
$tpl->assign("xfarr", $xfarr);
$tpl->assign("tdarr", $tdarr);
$tpl->assign("tdarr1", $tdarr1);
$tpl->assign("txarr", $txarr);

// 显示模板
$tpl->show("invitation_center");
?>
